package ui.dialog;

import java.io.InputStream;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableColumn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ui.util.IStructuredViewer;

import com.swtxml.swt.byid.ById;
import com.swtxml.views.SwtXmlTitleAreaDialog;


public class DialogColumnConfig extends SwtXmlTitleAreaDialog implements IStructuredViewer {
	private final static Logger logger = LoggerFactory
			.getLogger(DialogColumnConfig.class);

	private TableViewer tableViewer;
	private int colNum;
	private int vColNum = 0;
	private int[] order;
	private int defaultWidth;
	private Image img;

	@ById
	private List vList;
	@ById
	private List nvList;
	@ById
	private Button upButton;
	@ById
	private Button downButton;
	@ById
	private Button hideButton;

	@ById
	private Button restoreButton;

	public DialogColumnConfig(Shell parentShell) {
		super(parentShell);

	}

	public DialogColumnConfig(Shell parentShell, TableViewer tableViewer) {
		super(parentShell);
		this.tableViewer = tableViewer;
		this.colNum = tableViewer.getTable().getColumnCount();
		this.order = new int[colNum];
		this.defaultWidth = Math.max(100, tableViewer.getTable().getColumn(0)
				.getWidth());
		// this.img = new Image(device, filename)

	}

	@Override
	public TableViewer getTableViewer() {
		return tableViewer;
	}

	@Override
	public TreeViewer getTreeViewer() {
		return null;
	}

	@Override
	public void create() {
		super.create();
		// Set the title
		getContents().getShell().setText("Configure Columns");
//		Set Shell Title Icon
		InputStream inputStream =getClass().getResourceAsStream("/icons/used/index.gif");
		getContents().getShell().setImage(new Image(getContents().getDisplay(),	inputStream));

//		Title Area Text and Image	
		InputStream tInStream =getClass().getResourceAsStream("/icons/used/personal_loan.png");
		setTitle("Show / Hide Colmuns and Arrange column order");
		setTitleImage(new Image(getContents().getDisplay(),	tInStream));

		// Set the message
		// setMessage("This is a TitleAreaDialog", 0);
	}
	
	@Override
	protected void setupView() {
		TableColumn col;
		int vCnt = 0;
		int uvCnt = 0;
		int[] initOrder = tableViewer.getTable().getColumnOrder();
		
		for (int i = 0; i < colNum; i++) {
			col = tableViewer.getTable().getColumn(initOrder[i]);
//			 logger.debug("col all  uztest:{},{}",initOrder[i],col.getText());
			if (col.getWidth() == 0) {
				nvList.add(col.getText());
				order[colNum - uvCnt - 1] = tableViewer.getTable()
						.getColumnOrder()[i];
				uvCnt = uvCnt + 1;
			} else {
				vList.add(col.getText(), vCnt);
				order[vCnt] = tableViewer.getTable().getColumnOrder()[i];
				vCnt = vCnt + 1;
			}
		}
		
	}

	@Override
	protected void okPressed() {

		tableViewer.getTable().setColumnOrder(order);
		// for (int i = 0; i < colNum; i++) {
		// logger.debug("uztest: {},{}"
		// ,tableViewer.getTable().getColumnOrder()[i],order[i]);
		// logger.debug("uztest: {}" ,tableViewer.getTable().getColumn(i));
		// logger.debug("uztest: {},{}",
		// tableViewer.getTable().getColumnOrder()[i],
		// tableViewer.getTable().getColumn(order[i]));
		// }
		for (TableColumn tableColumn : tableViewer.getTable().getColumns()) {
			for(String aa: vList.getItems()){
				if (aa.equals(tableColumn.getText())) {
					tableColumn.setWidth(defaultWidth);
					break;
				}
			}
			for (String bb : nvList.getItems()) {
				if (bb.equals(tableColumn.getText())) {
					tableColumn.setWidth(0);
					break;
				}
			}
		}
		super.okPressed();
	}

	@Override
	protected boolean isResizable() {
		return true;
	}

	// ******************** Event Method************************
	private void upButtonClick() {
		handleUpDown(vList, true);
	}

	private void downButtonClick() {
		handleUpDown(vList, false);
	}

	private void hideButtonClick() {
		handleHideAndShow(vList, nvList);
	}
	private void showButtonClick() {
		handleHideAndShow(nvList, vList);
	}

	private void restoreButtonClick() {
		vList.removeAll();
		nvList.removeAll();
		for (int i = 0; i < colNum; i++) {
			vList.add(tableViewer.getTable().getColumn(i).getText());
			order[i] = i;
			tableViewer.getTable().getColumn(i).setWidth(defaultWidth);
		}
	}

	// *************private method**************************
	private void handleUpDown(List list, boolean up) {
		int index = list.getSelectionIndex();
		int newIndex = index + (up ? -1 : 1);
//		System.out.println(index + "index uztest" + newIndex);

		if (index >= 0 && newIndex >= 0 && newIndex < list.getItemCount()) {
			int maxIndex = Math.max(index, newIndex);
			String temp = list.getItem(maxIndex);
			list.remove(maxIndex);
			list.add(temp, maxIndex - 1);

			int cnt = order[newIndex];
			order[newIndex] = order[index];
			order[index] = cnt;

			for (int i = 0; i < list.getItemCount(); i++) {
				logger.debug("list :{}", list.getItem(i));
			}
			list.setSelection(newIndex);
//			handleSelectionChanged(newIndex);
		}
	}

	private void handleHideAndShow(List fromList, List toList) {
		for(int i=0; i< fromList.getSelectionCount();i++){
//			logger.debug("Show:{},{}", i,fromList.getSelection()[i]);
			toList.add(fromList.getSelection()[i], toList.getItemCount());
		}
		fromList.remove(fromList.getSelectionIndices());
		fromList.setSelection(0);

	}

}
